package com.topfinance.cnaps2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import com.topfinance.cnaps2.dto.SysOperDto;
import com.topfinance.cnaps2.dto.SysOperExDto;
import com.topfinance.cnaps2.ebo.SysFuncsEbo;
import com.topfinance.cnaps2.ebo.SysOperEbo;
import com.topfinance.cnaps2.ebo.SysUserEbo;
import com.topfinance.cnaps2.service.ISysOperManager;
import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * 
 * @author
 * 
 */

public class SysOperManagerImpl extends HibernateEntityDao<SysOperEbo> implements ISysOperManager {

	private static final Log log = LogFactory.getLog(SysOperManagerImpl.class);

	public void addOper(SysOperEbo o) throws AppException {
		save(o);
	}

	public void addOper(SysOperDto opt) throws AppException {
		SysFuncsEbo f = (SysFuncsEbo) get(SysFuncsEbo.class, opt.getFID());
		SysUserEbo u = (SysUserEbo) get(SysUserEbo.class, opt.getUID());
		SysOperEbo op = new SysOperEbo();
		BeanUtils.copyProperties(opt, op);
		op.setFunc(f);
		op.setUser(u);
		save(op);
	}

	public void editOper(SysOperDto opt) throws AppException {
		SysFuncsEbo f = (SysFuncsEbo) get(SysFuncsEbo.class, opt.getFID());
		SysUserEbo u = (SysUserEbo) get(SysUserEbo.class, opt.getUID());
		SysOperEbo op = new SysOperEbo();
		BeanUtils.copyProperties(opt, op);
		op.setFunc(f);
		op.setUser(u);
		update(op);
	}
	
	// 获得传输协议
	private SysOperEbo getOper(Integer fid,Integer uid) throws AppException {
		String hql = " from SysOperEbo t where t.user.ID = " + uid + " and t.func.FID = " + fid;
		log.debug("[getOper]" + hql);
		List rset = find(hql);
		if(rset.size() == 1)
			return (SysOperEbo)rset.get(0);
		else
			return null;
	}

	// 获得传输协议
	public SysOperEbo getOper(Integer fid) throws AppException {
		return (SysOperEbo) get(fid);
	}

	public void deleteOper(SysOperEbo o) throws AppException {
		removeById(o.getClass(), o.getID());
	}

	public void deleteOper(Integer id) throws AppException {
		removeById(SysOperEbo.class, id);
	}

	// 获得传输协议列表
	public List getAllOperList() throws AppException {
		String hql = " from SysOperEbo t ";
		String where = "";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where;
		return find(hql, paramList.toArray());
	}

	// 获得用户操作列表
	// 返回SysOperExDto对象列表
	public List getUserOperList(String uid) throws AppException {
		String hql = " select count(*) from SysOperEbo t where t.user.ID = " + uid;
		List res = find(hql);
		List resList;
		int count = Integer.parseInt(res.get(0).toString());
		//Integer fid, Integer uid, String name, String url, String desc, String enabled
		if (count > 0) {
			hql = "select new com.topfinance.cnaps2.dto.SysOperExDto";
			hql += "(t.func.FID,t.user.ID,t.func.name,t.func.url,t.func.desc,t.enabled)";
			hql += " from SysOperEbo t";
			hql += " where t.user.ID = " + uid;
			resList = find(hql);
		} else {
			hql = "select new com.topfinance.cnaps2.dto.SysOperExDto";
			//hql += "(t.FID,\"" + uid + "\",t.name,t.url,t.desc,\"N\")";
			hql += "(t.FID,t.name,t.url,t.desc)";
			hql += " from SysFuncsEbo t";
			resList = find(hql);
			for (Object oper : resList) {
				SysOperExDto op = (SysOperExDto) oper;
				op.setUID(new Integer(uid));
				op.setEnabled("N");
			}
		}
		return resList;
	}

	// 获得用户操作列表
	// 返回SysOperExDto对象列表
	public void modifyUserOper(String uid,String fids,String enables) throws AppException {
		String hql = " select count(*) from SysOperEbo t where t.user.ID = " + uid;
		List rset = find(hql);
		String afids[]= null;
		String enbls[]= null;
		
		if(fids != null && !"".equals(fids)){
			afids = fids.split(",");
		}
		if(enables != null && !"".equals(enables)){
			enbls = enables.split(",");
		}
		
		int count = Integer.parseInt(rset.get(0).toString());
		if (count > 0) {
			for(int i=0;i<afids.length;i++){
				SysOperEbo op = getOper(new Integer(afids[i]),new Integer(uid));
				if(op != null){
					op.setEnabled(enbls[i]);
					update(op);
				}
			}
		} else {
			for(int i=0;i<afids.length;i++){
				SysOperDto op = new SysOperDto(new Integer(uid),new Integer(afids[i]));
				op.setEnabled(enbls[i]);
				addOper(op);
			}			
		}
	}
}
